version 15.1

clear
use "worms_replication.dta"
set more off

//code each diocese by the alignment of its first bishop of the Concordats period
sort iddiocese year
***diocese covered by Concordats***
by iddiocese: gen z=1 if religious_bishop ~=. & Concordats == 1 
***dioceses not covered by Concordats***
replace  z = 1 if religious_bishop ~=. & covered == 0 & year >= 1122
by iddiocese: replace z=sum(z)
egen min_z_yr = min(year) if z == 1, by(iddiocese)
gen first_bishop = 1 if year == min_z_yr
***first bishop secular***
gen first_sec = 1 if first_bishop == 1 & religious_bishop == 0
***first bishop religious***
replace first_sec = 0 if first_bishop == 1 & religious_bishop == 1

//generate the relevant interactions
gen covered_first = covered*religious_bishop if first_bishop == 1
gen covered_sec = covered* first_sec if first_bishop == 1
gen sec_bishop = 1 - religious_bishop

//code each diocese by whether it is closer or further than the median from Rome
gen rome_far = 0
sum kmtorome if covered == 1 & diocese_count == 1, d
replace rome_far = 1 if kmtorome >= r(p50) & covered == 1 & diocese_count == 1
sum kmtorome if covered == 0 & diocese_count == 1, d
replace rome_far = 1 if kmtorome >= r(p50) & covered == 0 & diocese_count == 1

//rescale distance
gen mmtorome = kmtorome/1000
gen mm_covered = mmtorome * covered

gen covered_far = covered*rome_far

//Make Table 2: Change in population in covered vs uncovered dioceses that had religious vs secular first bishops or were far or close to rome
reg diff_pop covered_sec covered sec_bishop  if first_bishop == 1
reg diff_pop covered_far covered rome_far  if diocese_count == 1
reg diff_pop mm_covered covered mmtorome  if diocese_count == 1
